package com.zhuzi;

import cn.hutool.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.junit.jupiter.api.Test;

import java.io.IOException;

public class HttpClientDemo {

    @Test
    public void send() {

        System.out.println(hoolGet("www.example.com"));
    }

    public static String hoolGet(String url) {
        String body = null;
        try {
            body = HttpRequest.get(url).setConnectionTimeout(100).timeout(100).execute().body();
        } catch (Exception e) {
        }
        return body;
    }

    public static String get() {
        //获取DefaultHttpClient请求
        HttpClient client = HttpClientBuilder.create().build();

        HttpGet httpGet = new HttpGet("");
        String result = "";
        try {
            HttpResponse response = client.execute(httpGet);
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                result = EntityUtils.toString(response.getEntity());
            }
        } catch (IOException e) {
        }
        return result;
    }
}
